/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager.respoitory;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class InstalledBundleContentProvider extends ContentProvider {

    private SQLiteDatabase database;

    private Context context;

    public boolean onCreate() {
        return (context != null && database != null);
    }

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sort) {
        SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();
        sqLiteQueryBuilder.setTables("installed_bundle");
        Cursor c = sqLiteQueryBuilder.query(database, projection, selection, selectionArgs, null, null, null);
        c.setNotificationUri(context.getContentResolver(), uri);
        return c;
    }

    public Cursor queryByBundleId(Uri uri, String bundleId) {
        SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();
        sqLiteQueryBuilder.appendWhere("bundleId='" + bundleId + "'");
        sqLiteQueryBuilder.setTables("installed_bundle");
        Cursor c;
        try {
            c = sqLiteQueryBuilder.query(database, null, null, null, null, null, null);
        } catch (Exception e) {
            throw new SQLiteException(e.getMessage());
        }
        c.setNotificationUri(context.getContentResolver(), uri);
        return c;
    }

    public String getType(Uri uri) {
        throw new UnsupportedOperationException("This method is not supported");
    }

    public Uri insert(Uri uri, ContentValues contentValues) {
        Cursor c = queryByBundleId(uri, contentValues.getAsString("bundleId"));
        if (c.count() > 0)
            return null;
        Log.i(InstalledBundleContentProvider.class.getName(), "Inserting row: Bundle Id = "
                + contentValues.getAsString("bundleId"));
        long rowId = database.insert("installed_bundle", "bundleId", contentValues);
        if (rowId > 0) {
            Uri u = ContentUris.withAppendedId(Uri.parse("content://org.slideme/installed_bundle"), rowId);
            context.getContentResolver().notifyChange(u, null);
            return u;
        }

        throw new SQLException("Failed to insert row");
    }

    public int delete(Uri uri, String s, String[] strings) {
        throw new UnsupportedOperationException("This method is not supported");
    }

    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        throw new UnsupportedOperationException("This method is not supported");
    }

    public void init(Context context, SQLiteDatabase database) {
        if (database == null) {
            throw new IllegalArgumentException("database");
        }
        this.context = context;
        this.database = database;
    }
}
